﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SurveyHost.Models;
using System.Threading;

namespace SurveyHost.Tests.Integration.Models
{
    /// <summary>
    /// Summary description for Paragraph
    /// </summary>
    [TestClass]
    public class ParagraphTest
    {
        public ParagraphTest()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                if (ctx.DatabaseExists())
                    ctx.DeleteDatabase();
                ctx.CreateDatabase();
            }
        }

        [TestInitialize()]
        public void MyTestInitialize()
        {
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                ctx.Paragraphs.DeleteAllOnSubmit(ctx.Paragraphs);
                ctx.SubmitChanges();
            }

        }

        [TestMethod]
        public void CreateOverload()
        {
                var culture = Thread.CurrentThread.CurrentCulture.Name;
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.IsTrue(ctx.Paragraphs.Count() == 0);

                var result = Paragraph.Create("foo", "bar", culture);
                Assert.AreEqual("bar", result.Text);
                Assert.AreEqual("foo", result.Name);
                Assert.AreEqual(culture, result.Locale);
            }
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.IsTrue(ctx.Paragraphs.Count() == 1);
                Assert.AreEqual("bar", ctx.Paragraphs.First().Text);
                Assert.AreEqual("foo", ctx.Paragraphs.First().Name);
                Assert.AreEqual(culture, ctx.Paragraphs.First().Locale);
            }
        }

        [TestMethod]
        public void Create()
        {
            var culture = Thread.CurrentThread.CurrentCulture.Name;
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.IsTrue(ctx.Paragraphs.Count() == 0);

                var result = new Paragraph { Name = "foo", Text = "bar", Locale = culture };
                Paragraph.Create(result);
            }
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.IsTrue(ctx.Paragraphs.Count() == 1);
                Assert.AreEqual("bar", ctx.Paragraphs.First().Text);
                Assert.AreEqual("foo", ctx.Paragraphs.First().Name);
                Assert.AreEqual(culture, ctx.Paragraphs.First().Locale);
            }
        }

        [TestMethod]
        public void Delete()
        {
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.IsTrue(ctx.Paragraphs.Count() == 0);
                var culture = Thread.CurrentThread.CurrentCulture.Name;
                var result = new Paragraph { Name = "foo", Text = "bar", Locale = culture };
                ctx.Paragraphs.InsertOnSubmit(result);
                ctx.SubmitChanges();
                Assert.IsTrue(ctx.Paragraphs.Count() == 1);

                Paragraph.Delete(result);
            }
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.IsTrue(ctx.Paragraphs.Count() == 0);
            }
        }

        [TestMethod]
        public void Update()
        {
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.IsTrue(ctx.Paragraphs.Count() == 0);
                var culture = Thread.CurrentThread.CurrentCulture.Name;
                var result = new Paragraph { Name = "foo", Text = "bar", Locale = culture };
                ctx.Paragraphs.InsertOnSubmit(result);
                ctx.SubmitChanges();
                Assert.IsTrue(ctx.Paragraphs.Count() == 1);

                var temp = Paragraph.Retrieve("foo");
                temp.Text = "lol";
                Paragraph.Update(temp);
            }
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.AreEqual("lol", ctx.Paragraphs.Single().Text);
            }
        }

        [TestMethod]
        public void CreateMany()
        {
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.IsTrue(ctx.Paragraphs.Count() == 0);
                var culture = Thread.CurrentThread.CurrentCulture.Name;
                var first = new Paragraph { Name = "foo", Text = "bar", Locale = culture };
                var second = new Paragraph { Name = "bar", Text = "foo", Locale = culture };
                var many = new Paragraph[] { first, second };

                Paragraph.Create(many);
            }
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.IsTrue(ctx.Paragraphs.Count() == 2);
                Assert.IsTrue(ctx.Paragraphs.Where(x => x.Name == "foo").Single().Text == "bar");
                Assert.IsTrue(ctx.Paragraphs.Where(x => x.Name == "bar").Single().Text == "foo");
            }
        }

        [TestMethod]
        public void Get()
        {
            Paragraph[] result = null;
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.IsTrue(ctx.Paragraphs.Count() == 0);
                var culture = Thread.CurrentThread.CurrentCulture.Name;
                var first = new Paragraph { Name = "foo", Text = "bar", Locale = culture };
                var second = new Paragraph { Name = "bar", Text = "foo", Locale = culture };
                var many = new Paragraph[] { first, second };
                ctx.Paragraphs.InsertAllOnSubmit(many);
                ctx.SubmitChanges();
                Assert.IsTrue(ctx.Paragraphs.Count() == 2);

                result = Paragraph.Get();
            }
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.AreEqual(result.Length, 2);
                Assert.AreEqual("foo", result.Single(x => x.Name == "bar").Text);
                Assert.AreEqual("bar", result.Single(x => x.Name == "foo").Text);
            }
        }

        [TestMethod]
        public void DeleteMany()
        {
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.IsTrue(ctx.Paragraphs.Count() == 0);
                var culture = Thread.CurrentThread.CurrentCulture.Name;
                var first = new Paragraph { Name = "foo", Text = "bar", Locale = culture };
                var second = new Paragraph { Name = "bar", Text = "foo", Locale = culture };
                var many = new Paragraph[] { first, second };
                ctx.Paragraphs.InsertAllOnSubmit(many);
                ctx.SubmitChanges();
                Assert.IsTrue(ctx.Paragraphs.Count() == 2);

                Paragraph.Delete(many);
            }
            using (SurveyHost.Models.DataModelsContext ctx = new SurveyHost.Models.DataModelsContext())
            {
                Assert.IsTrue(ctx.Paragraphs.Count() == 0);
            }
        }
    }
}
